import Vue from 'vue'
import Vuex from 'vuex'
import Api from '@/utils/request'
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    token:uni.getStorageSync('token')?uni.getStorageSync('token'):'',
    user:uni.getStorageSync('user')?uni.getStorageSync('user'): {  },
  },
  mutations: {
    updateToken(state,token=''){
      uni.setStorageSync('token',token)
      state.token=token
    },
    updateUser(state,user){
      if(user){
        uni.setStorageSync('user',user)
      }else{
        uni.removeStorageSync('user')
      }
      state.user=user
    }
  },
  actions:{
    updateToken(context,payload){
      context.commit('updateToken',payload)
    },
    logout(context){
      context.commit('updateToken','')
      context.commit('updateUser', null)
    },
    async getInfo(context){
      let res = await Api('member/info')
      if(res&&res.code===200){
        context.commit('updateUser',res.data)
      }
    }
  }
})

export default store